package net.wofly.right.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.ReturnBean;
import net.wofly.right.domain.FuncList;
import net.wofly.right.dto.FuncModuleDto;
import net.wofly.right.service.IFuncListService;
import net.wofly.right.validator.FuncListValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.persistence.Transient;

@Controller
@RequestMapping(value = "/api/right/func")
@Api(description = "功能列表")
public class FuncListController {
    
    private static final String DEFAULT_PAGE_NUM = "1";
    private static final String DEFAULT_PAGE_SIZE = "10";
    Logger logger = LoggerFactory.getLogger(FuncListController.class);
    
    @Autowired
    IFuncListService iFuncListService;
    
    @ApiOperation(value = "获取功能模块分类")
    @RequestMapping(value = "/findAllFuncModules", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<FuncModuleDto>> findAllFuncModules() {
        return iFuncListService.findAllFuncModules();
    }
    
    /**
     * 添加功能
     *
     * @param funcList
     * @return
     */
    @ApiOperation(value = "添加功能")
    @ApiImplicitParams({@ApiImplicitParam(name = "funcList", value = "功能实体", required = true, dataType ="FuncList")})
    @RequestMapping(value = "/addFuncList", method = {RequestMethod.POST})
    @ResponseBody
    ReturnBean<FuncList> addFuncList(String funcListID,String funcName,String funcModule,Integer funcType,String funcCode, String funcUrl,String descp) {
        String errorMessage = FuncListValidator.validateAddFuncListParams(funcName, funcModule, funcType, funcUrl);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iFuncListService.addFuncList(funcName, funcModule, funcType, funcCode,  funcUrl, descp);
    }
    
    /**
     * 修改功能
     *
     * @param funcList
     * @return
     */
    @ApiOperation(value = "修改功能")
    @ApiImplicitParams({@ApiImplicitParam(name = "funcList", value = "功能实体", required = true, dataType =
            "FuncList")})
    @RequestMapping(value = "/modifyFuncList", method = {RequestMethod.POST})
    @ResponseBody
    ReturnBean<FuncList> modifyFuncList(@RequestBody FuncList funcList) {
        String errorMessage = FuncListValidator.validateModifyFuncListParams(funcList.getFuncListID(), 
        		funcList.getFuncName(), funcList.getFuncModule(), funcList.getFuncType(), funcList.getFuncUrl());
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iFuncListService.modifyFuncList(funcList);
    }
    
    /**
     * 根据条件查询功能列表
     *
     * @param funcListName
     * @param funcType
     * @return
     */
    @ApiOperation(value = "根据条件查询功能列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "funcName", value = "功能名称", dataType = "String", paramType ="form"), 
    						@ApiImplicitParam(name = "funcType", value = "功能类型（1：接口；2路由；）", allowableValues ="1,2", dataType = "int", paramType = "form"), 
    						@ApiImplicitParam(name = "pageNum", value ="页码（默认第一页）", dataType = "int", paramType = "form"), 
    						@ApiImplicitParam(name = "pageSize",value = "每页显示条数（默认10条）", dataType = "int", paramType = "form")})
    @RequestMapping(value = "/getFuncListByCond", method = {RequestMethod.POST})
    @ResponseBody
    ReturnBean<PageWrapper<FuncList>> getFuncListByCond(
            String funcName, Integer funcType, @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
            @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        return iFuncListService.getFuncListByCond(funcName, funcType, pageNum, pageSize);
    }
    
}
